<?php
namespace app\mini_api\controller;

class Exam extends Permission
{
    public function _initialize()
    {
        parent::_initialize();
        parent::check();
    }

    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $pageSize = isset($this->input_data['pageSize']) ? $this->input_data['pageSize'] : 15;

        $now = date('Y-m-d');
        $map = [
            'driver_id' => $this->user['id'],
            'b.s_time' => ['<=',$now],
            'b.e_time' => ['>=',$now],
        ];

        $exams = db('DriverExam')->alias('a')->join('exam b','a.exam_id = b.id','LEFT')->field('a.*,b.title,b.quantity,b.duration,b.s_time,b.e_time')->where($map)->order('id', 'desc')->page($page,$pageSize)->select();

        $total = db('DriverExam')->alias('a')->join('exam b','a.exam_id = b.id','LEFT')->field('a.*,b.title,b.quantity,b.s_time,b.e_time')->where($map)->count();
        $list = []; 

        foreach ($exams as $v) {
            $list[] = $v;
        }

        $data = [
            'list' => $list,
            'total' => $total
        ];
        $this->resSuc($data);
    }

    // 查看考试结果
    public function getResult()
    {
        $exam = db('DriverExam')->where(['id'=>$this->input_data['id']])->find();

        $map = [
            'driver_exam_id' => $this->input_data['id']
        ];

        $rows = db('DriverExamQuestion')->alias('a')->join('question b','a.question_id = b.id','LEFT')->field('a.*,b.result,b.type')->where($map)->select();

        $danxuan = $danxuanCorrect = $danxuanWrong = $danxuanScore = $duoxuan = $duoxuanCorrect = $duoxuanWrong = $duoxuanScore = $panduan = $panduanCorrect = $panduanWrong = $panduanScore = $panduanScore = 0;

        foreach ($rows as $v) {
            # 判断是什么类型的题目
            $type = getQuestionType($v);
            if($type == '单选题'){
                $danxuan++;
                if($v['score'] > 0)
                {
                    $danxuanCorrect++;
                    $danxuanScore += $v['score'];
                }
                else
                {
                    $danxuanWrong++;
                }
            }
            if($type == '多选题'){
                $duoxuan++;
                if($v['score'] > 0)
                {
                    $duoxuanCorrect++;
                    $duoxuanScore += $v['score'];
                }
                else
                {
                    $duoxuanWrong++;
                }
            }
            if($type == '判断题'){
                $panduan++;
                if($v['score'] > 0)
                {
                    $panduanCorrect++;
                    $panduanScore += $v['score'];
                }
                else
                {
                    $panduanWrong++;
                }
            }
        }

        $result = [
            [
                'type' => '单选题',
                'correct' => $danxuanCorrect,
                'wrong' => $danxuanWrong,
                'score' => $danxuanScore
            ],
            [
                'type' => '多选题',
                'correct' => $duoxuanCorrect,
                'wrong' => $duoxuanWrong,
                'score' => $duoxuanScore
            ],
            [
                'type' => '判断题',
                'correct' => $panduanCorrect,
                'wrong' => $panduanWrong,
                'score' => $panduanScore
            ],
            [
                'type' => '总计',
                'correct' => $panduanCorrect + $duoxuanCorrect + $danxuanCorrect,
                'wrong' => $danxuanWrong + $duoxuanWrong + $panduanWrong,
                'score' => $danxuanScore + $duoxuanScore + $panduanScore
            ]
        ];

        $data = [
            'score' => $exam['score'],
            'duration' => secToTime($exam['duration']),
            'quantity' => $exam['quantity'],
            'result' => $result
        ];

        $this->resSuc($data);
    }

}
